package com.itany.corejava.day17;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class TestServerA {

    public static void main(String[] args) {

        ServerSocket ss=null;

        try{

            ss=new ServerSocket(8888);

            int i=1;

            while(true){

                Socket s=null;
                PrintWriter pw=null;

                try{
                    System.out.println("+++++++++++");
                    //使线程处于阻塞状态，当客户端创建Socket时回到就绪状态
                    s=ss.accept();
                    System.out.println("-----------");

                    // Thread.sleep(1000000);
                    System.out.println(s.getInetAddress());

                    pw=new PrintWriter(s.getOutputStream());

                    pw.println("你是第"+(i++)+"个访问者，你的ip是"+s.getInetAddress());

                    pw.flush();

                }catch (Exception e){
                    e.printStackTrace();
                }finally{
                    if(pw!=null){
                        pw.close();
                    }

                    if(s!=null){
                        s.close();
                    }
                }


            }


        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(ss!=null){
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
